// FXToolView.cpp : implementation of the CFXToolView class
//

#include "stdafx.h"
#include "FXTool.h"
#include <d3dx9.h>
#include <D3D9.h>

#include "dxutil.h"
#include "d3dutil.h"
#include "d3dfont.h"
#include "D3DEnumeration.h"
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include "d3dutil.h"
#include <AniContainer.h>
#include <D3dModel.h>

#include "FXToolDoc.h"
#include "FXToolView.h"

#include "ToolCamera.h"
#include "ToolApp.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFXToolView

//extern CNarcissusApp *g_Draw;
IMPLEMENT_DYNCREATE(CFXToolView, CView)

BEGIN_MESSAGE_MAP(CFXToolView, CView)
	//{{AFX_MSG_MAP(CFXToolView)
	ON_WM_MOUSEWHEEL()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFXToolView construction/destruction

CFXToolView::CFXToolView()
{
	// TODO: add construction code here
	m_D3DApp = NULL;
	m_IsLMouseButtonClick = FALSE;
}

CFXToolView::~CFXToolView()
{
}

BOOL CFXToolView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFXToolView drawing

void CFXToolView::OnDraw(CDC* pDC)
{
	CFXToolDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if( m_D3DApp )
		m_D3DApp->Render3DEnvironment();
}

/////////////////////////////////////////////////////////////////////////////
// CFXToolView printing

BOOL CFXToolView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CFXToolView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CFXToolView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CFXToolView diagnostics

#ifdef _DEBUG
void CFXToolView::AssertValid() const
{
	CView::AssertValid();
}

void CFXToolView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CFXToolDoc* CFXToolView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFXToolDoc)));
	return (CFXToolDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFXToolView message handlers
BOOL CFXToolView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	// TODO: Add your message handler code here and/or call default	
	
	int		gap;

	if( zDelta > 0 )
		m_D3DApp->m_pCamera->howFar+= 5;
	else
		m_D3DApp->m_pCamera->howFar-=5;
	if (m_D3DApp->m_pCamera->howFar<3)
		m_D3DApp->m_pCamera->howFar=3;
	if (m_D3DApp->m_pCamera->howFar>400)
		m_D3DApp->m_pCamera->howFar=400;

	m_D3DApp->Render3DEnvironment();	
	
	return CView::OnMouseWheel(nFlags, zDelta, pt);
}

void CFXToolView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_IsLMouseButtonClick = TRUE;

	m_OldMousePt = m_CurMousePt = point;
	
	CView::OnLButtonDown(nFlags, point);
}

void CFXToolView::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_IsLMouseButtonClick = FALSE;
	
	CView::OnLButtonUp(nFlags, point);
}

void CFXToolView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default

	if( !m_IsLMouseButtonClick )		return;

	D3DXMATRIX				mat;
	D3DXMATRIX				temp2;
	D3DXVECTOR3				ret, temp;

	m_CurMousePt = point;


	float hh=(float)(m_D3DApp->m_pCamera->camheight)+((float)point.y-(float)m_OldMousePt.y)/2;
	m_D3DApp->m_pCamera->camheight=(int)hh;
	if (m_D3DApp->m_pCamera->camheight>200)
		m_D3DApp->m_pCamera->camheight=200;
	if (m_D3DApp->m_pCamera->camheight<0)
		m_D3DApp->m_pCamera->camheight=0;

	D3DXMatrixRotationY(&temp2,-(-(float)point.x+(float)m_OldMousePt .x)/70        );
	m_D3DApp->m_pCamera->rotmat=m_D3DApp->m_pCamera->rotmat*temp2;

	int		gapx = point.x - m_OldMousePt.x;

//	m_D3DApp->m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_D3DApp->m_pCamera->m_ViewMat );	
	
	m_OldMousePt = m_CurMousePt;

	m_D3DApp->Render3DEnvironment();
	
	CView::OnMouseMove(nFlags, point);
}

void CFXToolView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CView::OnTimer(nIDEvent);
}
